<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" name="viewport" />
    <meta content="yes" name="apple-mobile-web-app-capable" />
    <meta content="black" name="apple-mobile-web-app-status-bar-style" />
    <meta content="telephone=no" name="format-detection" />
    <title>商机设置</title>
    <link rel="stylesheet" href="css/ydui.css">
    <link rel="stylesheet" href="css/style.css">
    <link rel="stylesheet" href="iconfont/iconfont.css">
    <script src="js/ydui.flexible.js"></script>
</head>

<body>
    <div class="g-view setupPage" id="setupPage">
        <div class="m-celltitle">设置您希望收到的商机类型</div>
        <div class="m-cell">
            <label class="cell-item" v-for="(item, index) in type_arry" :key="index">
                <span class="cell-left">{{item.name}}</span>
                <label class="cell-right">
                    <input :value="item.id" type="checkbox" name="checkbox" :checked="item.ischecked" />
                    <i class="cell-checkbox-icon"></i>
                </label>
            </label>
        </div>
        <button @click="setupSubmit" class="btn-block btn-primary">提交</button>
    </div>
    <footer class="m-tabbar tabbar-fixed">
        <a href="index.html" class="tabbar-item">
            <span class="tabbar-icon">
                <i style="font-size:25px" class="icon iconfont">&#xe617;</i>
            </span>
            <span class="tabbar-txt">询盘商机</span>
        </a>
        <a href="setup.html" class="tabbar-item tabbar-active">
            <span class="tabbar-icon">
                <i style="font-size:20px" class="icon iconfont">&#xe698;</i>
            </span>
            <span class="tabbar-txt">设置商机</span>
        </a>
        <a href="release.html" class="tabbar-item">
            <span class="tabbar-icon">
                <i style="font-size:22px" class="icon iconfont">&#xe662;</i>
            </span>
            <span class="tabbar-txt">发布商机</span>
        </a>
        <a href="vip.html" class="tabbar-item">
            <span class="tabbar-icon">
                <i style="font-size:25px" class="icon iconfont">&#xe67e;</i>
            </span>
            <span class="tabbar-txt">会员中心</span>
        </a>
    </footer>
    <script src="js/jquery-3.3.1.min.js"></script>
    <script src="js/vue.js"></script>
    <script src="js/ydui.js"></script>
    <script src="js/nuoxia.js"></script>
    <script>
        window.onload = function () {
            var token = window.localStorage.getItem("token")
            var setupPage = new Vue({
                el: "#setupPage",
                data: {
                    type_arry: []
                },
                filters: {

                },
                mounted: function () {
                    this.setupType();
                },
                methods: {
                    setupType: function () {
                        var _this = this;
                        var checkbox_arry = [];
                        $.ajax({
                            type: "get",
                            url: paraurl + "api/type/userset?tm=" + tm,
                            data: {
                                token: token
                            },
                            success: function (data) {
                                if (data.code == 1) {
                                    if (data.data.length > 0) {
                                        for (var i = 0; i < data.data.length; i++) {
                                            checkbox_arry.push(data.data[i].type_id);
                                        }
                                        $.ajax({
                                            type: "get",
                                            url: paraurl + "api/type/index?tm=" + tm,
                                            data: {
                                                token: token
                                            },
                                            success: function (data) {
                                                if (data.code == 1) {
                                                    var type_arry = data.data.rows;
                                                    for (var i = 0; i < type_arry.length; i++) {
                                                        for (var j = 0; j < checkbox_arry.length; j++) {
                                                            if (type_arry[i].id == checkbox_arry[j]) {
                                                                type_arry[i].ischecked = true;
                                                            }
                                                        }
                                                    }
                                                    _this.type_arry = type_arry;
                                                }
                                            }
                                        })
                                    }else{
                                        $.ajax({
                                            type: "get",
                                            url: paraurl + "api/type/index?tm=" + tm,
                                            data: {
                                                token: token
                                            },
                                            success: function (data) {
                                                if (data.code == 1) {
                                                    var type_arry = data.data.rows;
                                                    _this.type_arry = type_arry;
                                                }
                                            }
                                        })
                                    }
                                }
                            }
                        })
                    },
                    setupSubmit: function () {
                        var names = $("#setupPage input[name='checkbox']");
                        var values = [];
                        for (var i = 0; i < names.length; i++) {
                            if (names[i].checked) {
                                values.push(names[i].value)
                            }
                        }
                        values = values.toString();
                        $.ajax({
                            type: "post",
                            url: paraurl + "api/type/userset?token=" + token + "&tm=" + tm,
                            data: {
                                "type_ids": values
                            },
                            success: function (data) {
                                if (data.code == 1) {
                                    YDUI.dialog.toast(data.msg, 'success', 2000, function () {
                                    });
                                } else {
                                    YDUI.dialog.alert(data.msg);
                                }
                            }
                        })
                    }
                }
            })
        }
    </script>
</body>

</html>